package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Resource
    private DishMapper dishMapper;
    @Resource
    private DishFlavorMapper dishFlavorMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        //转换为实体类数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.ENABLE);
        //保存菜品数据
        dishMapper.insert(dish);
        //获取菜谱id
        Long dishId = dish.getId();

        //保存口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dishId);
        });
        dishFlavorMapper.insert(flavors);
    }
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishVOList = (Page<DishVO>) dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(dishVOList.getTotal(),dishVOList.getResult());
    }
    @Override
    public void deleteByIds(List<Long> ids) {
        //判断菜品是否在售
        for (Long id :ids) {
            Dish dish=dishMapper.seletById(id);
            if (dish.getStatus() == StatusConstant.ENABLE)
            {
                //抛出异常
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断菜品是否被关联套餐
            SetmealDish setmealDish = setmealDishMapper.selectByDishId(ids);
            if (setmealDish != null)
            {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        //批量删除菜品
        dishMapper.deleteByIds(ids);
            dishFlavorMapper.deleteByDishId(ids);
    }

    @Override
    public void updateStatus(String status ,Long id) {
        dishMapper.updateStatus(status,id);
    }

    @Override
    public DishVO seletById(Long id) {
        //查菜品
        Dish dish = dishMapper.seletById(id);
        //查菜品口味
        List<DishFlavor> dishFlavorList =dishFlavorMapper.seletByDishId(id);
        //返回组装数据
        DishVO dishVO =new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;

    }

    @Transactional
    @Override
    public void updateDish(DishDTO dishDTO) {
        //创建dish 对象copy数据进去
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //修改dish中的数据
        dishMapper.updateById(dish);
        //删除dishfavorl中的数据
        dishFlavorMapper.deleteByDishId(Collections.singletonList(dishDTO.getId()));
        //插入新修改的数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });

            dishFlavorMapper.insert(flavors);
        }
    }

    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.seletByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


}
